import request from '@/utils/request'

// 文件信息接口
export interface FileInfo {
  id: number
  userId?: number
  fileName: string
  fileSize: number | null
  fileType: string | null
  filePath?: string | null
  ossKey?: string | null
  md5?: string | null
  parentId: number
  isFolder: number
  status: number
  downloadCount?: number
  viewCount?: number
  createTime: string
  updateTime: string
  parentPath?: string
}

// 文件列表查询参数
export interface FileListParams {
  parentId?: number
  page?: number
  size?: number
  orderBy?: string
  orderType?: 'asc' | 'desc'
}

// 文件列表响应
export interface FileListResponse {
  content: FileInfo[]
  // 兼容后端分页字段
  total?: number
  totalElements?: number
  page?: number
  number?: number
  size: number
  pages?: number
  totalPages?: number
  first?: boolean
  last?: boolean
  numberOfElements?: number
  empty?: boolean
}

// 上传签名响应
export interface UploadSignatureResponse {
  policy: string
  signature: string
  accessid: string  // 后端返回的字段名
  host: string
  key: string       // 后端返回的字段名
  expire: number
  dir?: string
}

// 保存文件元数据参数
export interface SaveFileMetadataParams {
  fileName: string
  fileSize: number
  fileType: string
  ossKey: string
  parentId: number
  md5?: string  // 可选参数，如果后端不需要就不传
}

// 存储信息响应
export interface StorageInfoResponse {
  storageQuota: number
  usedStorage: number
  availableStorage: number
  usagePercentage: number
}

// 下载信息响应
export interface DownloadInfoResponse {
  downloadUrl: string
  fileName: string
  fileSize: number
  expireTime: number
}

// 预览信息响应
export interface PreviewInfoResponse {
  previewUrl: string
  fileName: string
  fileType: string
  fileSize: number
  supportPreview: boolean
  expireTime: number
}

// 获取文件列表
export const getFileList = (params: FileListParams): Promise<FileListResponse> => {
  return request.get('/file/list', { params })
}

// 获取上传签名
export const getUploadSignature = (params: {
  fileName: string
  fileSize: number
  contentType: string
  parentId: number
}): Promise<UploadSignatureResponse> => {
  return request.post('/file/upload-signature', null, { params })
}

// 保存文件元数据
export const saveFileMetadata = (params: SaveFileMetadataParams): Promise<FileInfo> => {
  return request.post('/file/save-metadata', null, { params })
}

// 创建文件夹
export const createFolder = (folderName: string, parentId: number): Promise<FileInfo> => {
  return request.post('/file/create-folder', null, {
    params: { folderName, parentId }
  })
}

// 删除文件
export const deleteFile = (fileId: number): Promise<void> => {
  return request.post(`/file/delete/${fileId}`)
}

// 重命名文件
export const renameFile = (fileId: number, newName: string): Promise<void> => {
  return request.post(`/file/rename/${fileId}`, null, {
    params: { newName }
  })
}

// 移动文件
export const moveFile = (fileId: number, targetParentId: number): Promise<void> => {
  return request.post(`/file/move/${fileId}`, null, {
    params: { targetParentId }
  })
}

// 获取存储信息
export const getStorageInfo = (): Promise<StorageInfoResponse> => {
  return request.get('/file/storage-info')
}

// 生成下载链接
export const generateDownloadUrl = (fileId: number): Promise<DownloadInfoResponse> => {
  return request.get(`/file/download/${fileId}`)
}

// 生成预览链接
export const generatePreviewUrl = (fileId: number): Promise<PreviewInfoResponse> => {
  return request.get(`/file/preview/${fileId}`)
}

// 回收站相关API

// 获取回收站文件列表
export const getRecycleFileList = (params: FileListParams): Promise<FileListResponse> => {
  return request.get('/file/recycle/list', { params })
}

// 恢复文件
export const restoreFile = (fileId: number): Promise<void> => {
  return request.post(`/file/recycle/restore/${fileId}`)
}

// 彻底删除文件
export const permanentDeleteFile = (fileId: number): Promise<void> => {
  return request.post(`/file/recycle/delete/${fileId}`)
}

// 清空回收站
export const clearRecycleBin = (): Promise<void> => {
  return request.post('/file/recycle/clear')
}